package com.example.demo;

import com.example.demo.src.PromotionStrategy;
import com.example.demo.src.impl.CashbackStrategy;
import com.example.demo.src.impl.EmptyStrategy;
import com.example.demo.src.impl.GroupbuyStrategy;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Easettle
 * Date:2020/5/8 23:22
 * Email:easettle@qq.com
 * 结合单例模式和简单工厂模式
 */
public class PromotionStrategyFactory {
    private static Map<String, PromotionStrategy> stringPromotionStrategyMap = new HashMap<>();

    static {
        stringPromotionStrategyMap.put(PromotionKey.CASHBACK, new CashbackStrategy());
        stringPromotionStrategyMap.put(PromotionKey.GROUPBUY, new GroupbuyStrategy());
        stringPromotionStrategyMap.put(PromotionKey.COUPON, new CashbackStrategy());
        stringPromotionStrategyMap.put(PromotionKey.EMPTY, new CashbackStrategy());
    }

//    private PromotionStrategyFactory() {
//    }

    private static final PromotionStrategy PROMOTION_STRATEGY = new EmptyStrategy();

    public static PromotionStrategy getPromotionStrategyFactory(String promotionKey) {
        PromotionStrategy promotionStrategy = stringPromotionStrategyMap.get(promotionKey);
        return promotionKey == null ? PROMOTION_STRATEGY : promotionStrategy;
    }

    private interface PromotionKey {
        String COUPON = "COUPON";
        String CASHBACK = "CASHBACK";
        String GROUPBUY = "GROUPBUY";
        String EMPTY = "EMPTY";
    }

    public static Set<String> getPromotionKeys() {
        return stringPromotionStrategyMap.keySet();
    }
}
